package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.PageResult;
import com.pinyougou.mapper.SpecificationOptionMapper;
import com.pinyougou.mapper.TypeTemplateMapper;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.pojo.TypeTemplate;
import com.pinyougou.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service(interfaceName = "com.pinyougou.service.TypeTemplateService")
@Transactional
public class TypeTemplateServiceImpl implements TypeTemplateService
{
    @Autowired
    private TypeTemplateMapper typeTemplateMapper;

    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    /**
     * 多条件分页查询
     *
     * @param typeTemplate
     * @param page
     * @param rows
     */
    @Override
    public PageResult findByPage(TypeTemplate typeTemplate, int page, int rows)
    {
        //return null;
        PageInfo<TypeTemplate> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect()
        {
            @Override
            public void doSelect()
            {
                typeTemplateMapper.findByLike(typeTemplate);
            }
        });
        return new PageResult(pageInfo.getPages(),pageInfo.getList());
    }

    /** 查找模板的id和name的集合 */
    @Override
    public List<Map<String, Object>> findIdNameList()
    {
        return typeTemplateMapper.findIdNameList();
    }

    @Override
    public List<Map> findSpecification(Long id)
    {
        //在type_template查找spec_ids
        String specIds = findOne(id).getSpecIds();
        //为什么不能指定类型Map<String,Object>
        List<Map> data = JSON.parseArray(specIds,Map.class);
        for(Map map : data)
        {
            Long specId = Long.valueOf(map.get("id").toString());
            //根据spec_ids查找specification_option查
            SpecificationOption specificationOption = new SpecificationOption();
            specificationOption.setSpecId(specId);
            List<SpecificationOption> specificationOptionList = specificationOptionMapper.select(specificationOption);
            map.put("options",specificationOptionList);
            //this.get(new HashMap<String,Object>().getClass());
            //this.get(HashMap.class);
            //Map<String,Object>
        }

        return data;
    }


    /**
     * 添加方法
     *
     * @param typeTemplate
     */
    @Override
    public void save(TypeTemplate typeTemplate)
    {
        typeTemplateMapper.insertSelective(typeTemplate);

    }

    /**
     * 修改方法
     *
     * @param typeTemplate
     */
    @Override
    public void update(TypeTemplate typeTemplate)
    {
        typeTemplateMapper.updateByPrimaryKeySelective(typeTemplate);

    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id)
    {

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteAll(Serializable[] ids)
    {
        Example example = new Example(TypeTemplate.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids));
        typeTemplateMapper.deleteByExample(example);
    }

    /**
     * 根据主键id查询
     *
     * @param id
     */
    @Override
    public TypeTemplate findOne(Serializable id)
    {
        return typeTemplateMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询全部
     */
    @Override
    public List<TypeTemplate> findAll()
    {
        return null;
    }


}